package com.orion.config.part;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator;

/**
 * @author Administrator
 * @date 2021/10/28
 */
public class CustomGatewayFilter implements GatewayFilter, Ordered {

    private AbstractNameValueGatewayFilterFactory.NameValueConfig config;

    public CustomGatewayFilter(AbstractNameValueGatewayFilterFactory.NameValueConfig config) {
        this.config = config;
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("<-------------- BEGIN in custom gateway filter -------------->");
        System.out.println("do something before");
        return chain.filter(exchange).then(Mono.fromRunnable(() -> {
            System.out.println("do something after");
            System.out.println("custom gateway filter : " + toString());
            System.out.println("<-------------- END in custom gateway filter -------------->");
        }));
    }

    @Override
    public int getOrder() {
        return 123;
    }

    @Override
    public String toString() {
        return filterToStringCreator(this)
                .append(config.getName(), config.getValue()).toString();
    }
}
